package avicit.bdp.lineage.druid.analyse.handler;

import avicit.bdp.lineage.druid.analyse.SqlRequestContext;
import avicit.bdp.lineage.druid.analyse.SqlResponseContext;
import avicit.bdp.lineage.druid.model.TableNode;
import avicit.bdp.lineage.druid.model.TreeNode;

/**
 * 填充信息
 * 1. 填充字段信息 setTableExpression(String)
 */
public class FillTableExpressHandler implements IHandler {

    @Override
    public void handleRequest(SqlRequestContext request, SqlResponseContext response) {
        fillingTableExpression(response.getLineageTableTree());
    }

    /**
     * Tree<Table> 填充Column的TableExpression 字段
     *
     * @param root 当前表关系树节点
     */
    public void fillingTableExpression(TreeNode<TableNode> root) {
        if (root.getValue() != null) {
            root.getValue().getColumns()
                    .forEach(columnNode -> columnNode.setTableExpression(root.getValue().getExpression()));
        }
        if (root.isLeaf()) {
            return;
        }
        root.getChildList().forEach(this::fillingTableExpression);
    }

}
